package cn.stylefeng.guns.modular.basic.controller;

import cn.stylefeng.guns.modular.basic.mapper.AreaMapper;
import cn.stylefeng.guns.modular.basic.model.params.AreaParas;
import cn.stylefeng.guns.modular.basic.model.result.AreaResult;
import cn.stylefeng.roses.kernel.model.validator.BaseValidatingParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.base.shiro.annotion.Permission;
import cn.stylefeng.guns.modular.basic.entity.BasicAddress;
import cn.stylefeng.guns.modular.basic.entity.BasicLine;
import cn.stylefeng.guns.modular.basic.entity.BasicPutgoods;
import cn.stylefeng.guns.modular.basic.model.params.BasicAddressParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicLineParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicAddressResult;
import cn.stylefeng.guns.modular.basic.service.BasicAddressService;
import cn.stylefeng.guns.modular.basic.service.BasicLineService;
import cn.stylefeng.guns.modular.basic.service.BasicPutgoodsService;
import cn.stylefeng.guns.modular.order.model.params.OrderTransportParam;
import cn.stylefeng.guns.modular.order.service.OrderTransportService;
import cn.stylefeng.guns.modular.transport.model.params.TransportReceiptParam;
import cn.stylefeng.guns.modular.transport.service.TransportReceiptService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;

import java.util.List;


/**
 * 往来地址表控制器
 */
@Controller
@RequestMapping("/basicAddress")
public class BasicAddressController extends BaseController {

    private String PREFIX = "/basic/basicAddress";

    @Autowired
    private BasicAddressService basicAddressService;

    @Autowired
    private BasicPutgoodsService basicPutgoodsService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicLineService basicLineService;
    @Autowired
    private AreaMapper areaMapper;

    /**
     * 跳转到 选择位置
     */
    @RequestMapping("toChoiceAddress")
    public String toChoiceAddress() {
        return PREFIX + "/selectlocation.html";
    }

    /**
     * 跳转到主页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/basicAddress.html";
    }

    /**
     * 新增页面
     */
    @RequestMapping("/add")
    public String add(@RequestParam("putgoodsId") Long putgoodsId,Model model) {
    	model.addAttribute("putgoodsId", putgoodsId);
    	User user = userService.getcurrentUser();
    	BasicLineParam basicLineParam = new BasicLineParam();
    	basicLineParam.setDeliverId(user.getDeliverId());
    	basicLineParam.setDeptId(user.getDeptId());
    	model.addAttribute("llist", basicLineService.findListBySpec(basicLineParam,user.getOperateId()));
        return PREFIX + "/basicAddress_add.html";
    }

	/**
     * 编辑页面
     */
    @RequestMapping("/edit")
    public String edit(Model model) {
    	User user = userService.getcurrentUser();
    	BasicLineParam basicLineParam = new BasicLineParam();
    	basicLineParam.setDeliverId(user.getDeliverId());
    	basicLineParam.setDeptId(user.getDeptId());
    	model.addAttribute("llist", basicLineService.findListBySpec(basicLineParam,user.getOperateId()));
        return PREFIX + "/basicAddress_edit.html";
    }

    /**
     * 新增接口
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BasicAddressParam basicAddressParam) {
    	basicAddressParam.setSwitchWl("1");
    	String address = basicAddressParam.getAddress();
    	if (address!=null&&address!="") {
    		address = address.replace("& #40;", "(");
    		address = address.replace("& #41;", ")");
    		basicAddressParam.setAddress(address);
    	}
    	if(basicAddressParam.getLineId()!=null) {
        	BasicLine basicLine = basicLineService.getById(basicAddressParam.getLineId());
        	basicAddressParam.setLineName(basicLine.getLineName());
    	}
    	BasicPutgoods basicPutgoods = basicPutgoodsService.getById(basicAddressParam.getPutgoodsId());
    	basicAddressParam.setPutgoodsName(basicPutgoods.getPutgoodsName());

        this.basicAddressService.add(basicAddressParam);
        return ResponseData.success();
    }

    @Autowired
    private OrderTransportService orderTransportService;

    @Autowired
    private TransportReceiptService transportReceiptService;

    /**
     * 编辑接口
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BasicAddressParam basicAddressParam) {
    	String address = basicAddressParam.getAddress();
    	if (address!=null&&address!="") {
    		address = address.replace("& #40;", "(");
    		address = address.replace("& #41;", ")");
    		basicAddressParam.setAddress(address);
    	}
    	if(basicAddressParam.getLineId()!=null) {
        	BasicLine basicLine = basicLineService.getById(basicAddressParam.getLineId());
        	basicAddressParam.setLineName(basicLine.getLineName());
        	
        	//更新货运订单线路
        	OrderTransportParam orderTransportParam = new OrderTransportParam();
        	orderTransportParam.setGetLocationId(Long.parseLong(basicAddressParam.getId().toString()));
        	orderTransportParam.setLineId(basicAddressParam.getLineId());
        	orderTransportParam.setLineName(basicAddressParam.getLineName());
        	orderTransportService.updateBySpec(orderTransportParam);
    	}
    	
    	//更新货运订单 收货地址信息
//    	OrderTransportParam orderTransportParam2 = new OrderTransportParam();
//    	orderTransportParam2.setGetLocationId(Long.parseLong(basicAddressParam.getId().toString()));
//    	orderTransportParam2.setGetLocation(basicAddressParam.getAddress());
//    	orderTransportParam2.setGetPeople(basicAddressParam.getContacts());
//    	orderTransportParam2.setGetTel(basicAddressParam.getTel());
//    	orderTransportParam2.setKilometer(basicAddressParam.getKilometer());
//    	orderTransportService.updateBySpec2(orderTransportParam2);
    	
    	//更新回传表 未回传地址
//    	TransportReceiptParam transportReceiptParam = new TransportReceiptParam();
//    	transportReceiptParam.setGetLocationid(Long.parseLong(basicAddressParam.getId().toString()));
//    	transportReceiptParam.setTstate("未回传");
//    	transportReceiptParam.setGetLocation(basicAddressParam.getAddress());
//    	transportReceiptParam.setGetPeople(basicAddressParam.getContacts());
//    	transportReceiptParam.setGetTel(basicAddressParam.getTel());
//    	transportReceiptService.updateBySpec(transportReceiptParam);
    	
        this.basicAddressService.update(basicAddressParam);
        return ResponseData.success();
    }

    /**
     * 围栏开关接口
     */
    @RequestMapping("/editItemswitchWl")
    @ResponseBody
    @Permission
    public ResponseData editItemswitchWl(BasicAddressParam basicAddressParam) {
        this.basicAddressService.update(basicAddressParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     */
    @RequestMapping("/delete")
    @ResponseBody
    @Permission
    public ResponseData delete(BasicAddressParam basicAddressParam) {
        this.basicAddressService.delete(basicAddressParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BasicAddressParam basicAddressParam) {
        BasicAddress detail = this.basicAddressService.getById(basicAddressParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查看详情接口
     */
    @RequestMapping("/getByPutgoodsId")
    @ResponseBody
    public ResponseData getByPutgoodsId(BasicAddressParam basicAddressParam) {
        BasicAddressResult detail = this.basicAddressService.findBySpec(basicAddressParam);
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BasicAddressParam basicAddressParam,String condition ) {
        return this.basicAddressService.findPageBySpec(basicAddressParam,condition);
    }




    @ResponseBody
    @RequestMapping("/list2")
    public List<AreaResult> list2(AreaParas areaParas){
        List<AreaResult> list = areaMapper.List2(areaParas);
        return list;
        }

    @ResponseBody
    @RequestMapping("/mapAddress")
    public List<BaseValidatingParam> mapAddress(){
        return this.basicAddressService.mapAddress();
    }
}


